#############################################################################################################################################################
### Setup 

# Source setup file
setwd('~/Dropbox/Projects/Dissertation/Bill Text and Agenda Control/JOP Replication Files')
source('text_setup.R')

# Load bills info
aw <- readRDS(file = 'Data/prepped_bills_rr.rds')
topics <- unique(aw$major) %>% sort() %>% .[!.==99]

# Load cosine similarity distribution
csA <- readRDS(file = 'Data/cosineSimilarityAnalysis_Combined_rr.rds')



##############################################################################################################################################################
### Format Data

### Load House and Senate data (and bill names) from most recent Congress in sample (for relevance)

# House
hcs <- fread('Data/cosineSimilarityMatrix_114_House_rr.csv', data.table = FALSE) %>% .[,-1]
hcsn <- readRDS('Data/allBillsweights_Names_114_House_rr.rds') %>%
  str_replace(., '_pf|_f', '')
rownames(hcs) <- names(hcs) <- hcsn

# Senate
scs <- fread('Data/cosineSimilarityMatrix_114_Senate_rr.csv', data.table = FALSE) %>% .[,-1]
scsn <- readRDS('Data/allBillsweights_Names_114_Senate_rr.rds') %>%
  str_replace(., '_pf|_f', '')
rownames(scs) <- names(scs) <- scsn

# List of bills in this congress and a chosen issue area (education)
congBills <- filter(aw, congress == 114 & major==9) %>% # works with major==8 for house similarity, also #9
  mutate(billID = paste0(congress, '_', bill_number)) %>%
  pull(billID)
congBillsDiss <- filter(aw, congress == 114 & major %in% c(3,4)) %>% 
  mutate(billID = paste0(congress, '_', bill_number)) %>%
  pull(billID)

# Filter scs and hcs
hcsF <- hcs[rownames(hcs) %in% congBills, names(hcs) %in% congBills]
scsF <- scs[rownames(scs) %in% congBills, names(scs) %in% congBills]

hcsD <- hcs[rownames(hcs) %in% congBillsDiss, names(hcs) %in% congBillsDiss]
scsD <- scs[rownames(scs) %in% congBillsDiss, names(scs) %in% congBillsDiss]


### Pull bills that fit criteria (most alike, least alike)

# Find the minimum/maximum/closest to zero cosine similarity in each
hmax <- max(hcsF[!hcsF==1])
smax <- max(scsF[!scsF==1])
hzero <- min(abs(hcsD))
szero <- min(abs(scsD))

# See which bill comparisons match these criteria
hmaxBills <- which(hcsF==hmax, arr.ind=TRUE) %>% rownames()
smaxBills <- which(scsF==smax, arr.ind=TRUE) %>% rownames()

hzeroBills <- which(abs(hcsD)==hzero, arr.ind=TRUE) %>% rownames()
szeroBills <- which(abs(scsD)==szero, arr.ind=TRUE) %>% rownames()

# Quantiles of distribution
dist <- c( hcs[lower.tri(hcs)] %>% as.matrix() %>% as.vector(),
           scs[lower.tri(scs)] %>% as.matrix() %>% as.vector()
)
ci99 <- quantile(dist, c(0.025, 1))



##############################################################################################################################################################
### Figure A2: Panel A

# Parameters
pdf(file='Figures/fgA2panelA.pdf', width=6, height=6)
par(xaxs='i',yaxs='i')
xlim <- c(-1,1)
ylim <- c(-1,1)
extra <- 0.1
plot(NA, xlim=xlim+extra*c(-1,1), ylim=ylim+extra*c(-1,1),
     axes=FALSE, ann=FALSE)

# Custom axes
xtick <- c(-1, 1)
ytick <- c(-1, 1)
tick.len <- 0.04
tick.zeroadd <- 0.1
segments(xtick, 0, xtick, -tick.len, lwd=2)
segments(0, ytick, -tick.len, ytick, lwd=2)
abline(h=0, lwd=2, lty=2)
abline(v=0, lwd=2, lty=2)

# Plot
x1 <- 0.90
vec1 <- c(x1, sqrt(1-x1^2))
bill1 <- hmaxBills[1] %>% str_replace('114_', '') %>% toupper()
angle1 <- atan(vec1[2]/vec1[1])
angle2 <- angle1+acos(hmax)
vec2 <- c(cos(angle2), sin(angle2))
bill2 <- hmaxBills[2] %>% str_replace('114_', '') %>% toupper()

points(vec1[1], vec1[2], pch=16)
lines(c(0, vec1[1]), c(0, vec1[2]), lwd=2)
points(vec2[1], vec2[2], pch=16)
lines(c(0, vec2[1]), c(0, vec2[2]), lwd=2)

draw.arc(0,0,0.3,angle1=angle1, angle2=angle2, lwd=2)

text(vec1[1],vec1[2], bill1, pos=3)
text(vec2[1], vec2[2], bill2, pos=2)
text(0.17, 0.20, expression(alpha), cex=1.5)

dev.off()



##############################################################################################################################################################
### Figure A2: Panel B

# Parameters
pdf(file='Figures/fgA2panelB.pdf', width=6, height=6)
par(xaxs='i',yaxs='i')
xlim <- c(-1,1)
ylim <- c(-1,1)
extra <- 0.1
plot(NA, xlim=xlim+extra*c(-1,1), ylim=ylim+extra*c(-1,1),
     axes=FALSE, ann=FALSE)

# Custom axes
xtick <- c(-1, 1)
ytick <- c(-1, 1)
tick.len <- 0.04
tick.zeroadd <- 0.1
segments(xtick, 0, xtick, -tick.len, lwd=2)
segments(0, ytick, -tick.len, ytick, lwd=2)
abline(h=0, lwd=2, lty=2)
abline(v=0, lwd=2, lty=2)

# Plot
x1 <- 0.98
vec1 <- c(x1, sqrt(1-x1^2))
bill1 <- szeroBills[1] %>% str_replace('114_', '') %>% toupper()
angle1 <- atan(vec1[2]/vec1[1])
angle2 <- angle1+acos(szero)
vec2 <- c(cos(angle2), sin(angle2))
bill2 <- szeroBills[2] %>% str_replace('114_', '') %>% toupper()

points(vec1[1], vec1[2], pch=16)
lines(c(0, vec1[1]), c(0, vec1[2]), lwd=2)
points(vec2[1], vec2[2], pch=16)
lines(c(0, vec2[1]), c(0, vec2[2]), lwd=2)

draw.arc(0,0,0.3,angle1=angle1, angle2=angle2, lwd=2)

text(vec1[1]-0.02,vec1[2], bill1, pos=3)
text(vec2[1], vec2[2], bill2, pos=2)
text(0.1, 0.25, expression(alpha), cex=1.5)

dev.off()



##############################################################################################################################################################
### Figure A2: Panel C

# Set up plot area
pdf(file='Figures/fgA2panelC.pdf', width=10, height=6)

# Plot the cosine curve
x <- seq(-0.5, 2*pi+0.5, 0.1)
y <- cos(x)
plot(x, y, type='n', xaxt='n', xlim=c(-0.6, 2*pi+0.5), xlab='', ylab='', ylim=c(-0.2, 1.3), yaxt='n', font=2)
lines(x, y, lwd=2, lty=2)
axis(side=2, at=c(-1, 0, 1), labels=c(-1,0,1), las=2)
abline(h=1, lty=2)
abline(h=0, lty=2)
title(ylab='Cosine Similarity', cex.lab=1.2)

# Plot dissimilar bills
points(acos(szero), szero, pch=16, cex=1.5)
maxBills <- str_replace(szeroBills, '114_', '') %>% toupper()
text(acos(szero), szero+0.05, pos=4, labels=paste0('c) ', maxBills[1], ' & ', maxBills[2]), font=2)

# Plot similar bills
points(acos(hmax), hmax, pch=16, cex=1.5)
maxBills <- str_replace(hmaxBills, '114_', '') %>% toupper()
text(acos(hmax), hmax, pos=4, labels=paste0('b) ', maxBills[1], ' & ', maxBills[2]), font=2)

# Plot same bill
points(0, 1, pch=16, cex=1.5)
text(0, 1, pos=3, labels='a) HR3588', font=2)

# Plot range
arrows(x0=2*pi, y0=min(ci99), x1=2*pi, y1=max(ci99), angle=90, lwd=3, length=0.15)
arrows(x0=2*pi, y0=max(ci99), x1=2*pi, y1=min(ci99), angle=90, lwd=3, length=0.15)
text(x=2*pi, y=0.5, pos=2, labels='99% Dist.', font=2)

dev.off()
